<?php
namespace app\common;
use yii;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class SendMailer
{
    /**
     * 发送邮件
     * @param $to
     * @param $subject
     * @param $params
     * @return bool
     */
    public function send($to,$to_name,$password)
    {
        /*$subject = "合作商密码";
        $message = "这是您最新的密码：".$password."。时间：".date('Y-m-d');
        $from = "admin@youoil.cn";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        return true;*/
        $config = Yii::$app->params['email'];
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = $config['host'];  // Specify main and backup SMTP servers
            //$mail->Host = 'smtp.qq.com';
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = $config['username'];                 // SMTP username
            //$mail->Username = '13247912@qq.com';
            $mail->Password = $config['password'];                           // SMTP password
            //$mail->Password = 'bc87082551';
            $mail->SMTPAutoTLS = false;
            //$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to
            //$mail->SMTPSecure = 'ssl';
            //$mail->Port = 465;

            //Recipients
            $mail->setFrom($config['from'], '中经汇通');
            //$mail->setFrom('13247912@qq.com', '乐驾包');
            $mail->addAddress($to, $to_name);     // Add a recipient
            /*$mail->addAddress('ellen@example.com');               // Name is optional
            $mail->addReplyTo('info@example.com', 'Information');
            $mail->addCC('cc@example.com');
            $mail->addBCC('bcc@example.com');*/


            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = '合作商密码';
            $mail->Body    = "这是您最新的密码：".$password."<br>时间：".date('Y-m-d H:i:s');

            $mail->send();
            return true;
        } catch (Exception $e) {
            return false;
        }

    }

}